<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>打包工具</title>
	<link rel = "icon" href = "Developer.ico">
	<script src="layDate/laydate/laydate.js"></script>
	<style>
		html, body {
			width: 100%;
			height: 100%;
			margin: 0;
			padding: 0;
		}
		body {
			background-color: rgb(58, 58, 58);
			color: #fff;
		}
		input {
			background-color: rgb(58, 58, 58);
			color: #fff;
			border: none;
			border-bottom: 1px solid rgb(143, 143, 143);
			outline: none;
			height: 30px;
		}
		input:focus {
			border-bottom: 1px solid rgb(143, 143, 143);
		}
		.coverAll {
			position: fixed;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
			z-index: 999;
		}
		.formTable {
			width: 100%;
			margin-top: 15px;
		}
		td.label {
			width: 100px;
		}
		#projectPath, #exportPath {
			width: calc(100% - 50px);
		}
		.button {
			width: 50px;
			height: 30px;
			background-color: rgb(78, 78, 78);
			color: #fff;
			border: none;
			cursor: pointer;
		}
		.button.delete {
			background-color: #ff7e7e;
		}
		button:focus {
			outline:none;
		}
		.exportFile {
			width: 100%;
			margin: 20px auto;
			display: block;
		}
		.console {
			position: relative;
			padding-top: 1px;
			box-sizing: border-box;
			outline: none;
		}
		.console-line {
			width: 100%;
			height: 1px;
			background-color: #fff;
			position: fixed;
			top: calc(100% - 200px);
			z-index: 1;
		}
		.workspace .item {
			background-color: rgb(100, 100, 100);
			margin-top: 2px;
			outline: none;
			height: 30px;
    		line-height: 30px;
			white-space: nowrap;
			overflow-x: auto;
		}
		.workspace .item::-webkit-scrollbar {
			display: none!important;
		}
		.workspace .item.active {
			background-color: rgb(58, 58, 58);
		}
		.workspace .item:hover {
			background-color: rgb(58, 58, 58);
		}
		.ignoreFile {
			width: calc(100% - 100px);
		}
		#menuDiv .button {
			width: 100px;
		}
	</style>
  </head>
<body oncontextmenu="return false;" onclick="bodyClick();">
	<div style="height: calc(100% - 200px);cursor: default;user-select: none;"  >
		<div style="width: 150px;height: 100%;float: left;overflow: auto;background-color: rgb(100, 100, 100);">
			<div class="workspace" id="workspaceDiv">
			</div>
			<div>
				<button class="button" style="width: 80%;margin: 10px 10%;" onclick="newWorkspace();">新建工作空间</button>
			</div>
		</div>
		<div style="width: calc(100% - 150px);height: 100%;display: inline-block;overflow: auto;padding: 0 5%;box-sizing: border-box;">
			<table class="formTable">
				<tr>
					<td class="label">项目路径</td>
					<td><input id="projectPath" value="D:/work/bjsw_oa/zf_bjsw_bg_bgtoa/"/><button class="button" onclick="selectPath('folder', 'projectPath');">选择</button></td>
				</tr>
				<tr>
					<td class="label">web路径</td>
					<td><input id="webroot" value="defaultroot"/></td>
				</tr>
				<tr>
					<td class="label">起始日期</td>
					<td><input id="startDate" value="2022-02-15"/> 至 <input id="endDate" value="2022-03-27"/></td>
				</tr>
				<tr>
					<td class="label">打包输出路径</td>
					<td><input id="exportPath" value="D:/workspace/"/><button class="button" onclick="selectPath('folder', 'exportPath');">选择</button></td>
				</tr>
				<tr>
					<td class="label">忽略文件</td>
					<td>
						<button class="button" onclick="addIgnoreFile();">添加</button>&nbsp;&nbsp;<span>请填写src路径（不是class路径）</span>
						<div id="ignoreFileDiv" class="ignoreFileDiv">
						</div>
					</td>
				</tr>
				<tr>
					<td class="label">全部导出</td>
					<td>
						<button class="button exportFile" onclick="exportFileClick();">全部导出</button>
					</td>
				</tr>
				<tr style="">
					<td class="label">按账号导出</td>
					<td>
						<button class="button" onclick="addGitUserName();">添加</button>
						<div id="gitUserNameDiv" class="gitUserNameDiv">
						</div>
					</td>
				</tr>
			</table>
			
			<!-- <button class="button" style="width: 100px;" onclick="saveWorkspace();">保存工作空间</button> -->
			<!-- <button class="button" style="width: 100px;background-color: #ff7e7e;" onclick="deleteWorkspace();">删除工作空间</button> -->
		</div>
	</div>
	<div class="console-line"></div>
	<div contenteditable="true" id="console" class="console" style="overflow: auto;height: 200px;" oncontextmenu="consoleMenu();">
	</div>
	<div id="loading" style="width: 100%;height: 100%;position: fixed;top:0;text-align: center;display: none;">
		<img src="loading.gif" style="position:relative; top: 44%;width: 120px;"/>
	</div>
</body>
<script>

	laydate.render({
		elem: '#startDate', //指定元素
		theme: '#828282'
	});
	laydate.render({
		elem: '#endDate', //指定元素
		theme: '#828282'
	});

	// 操作文件的
	var fs = require( 'fs' );
	// 引入子进程模块
	var childProcess = require('child_process');
	// 获取路径的
	var path = require('path');
	// uuid
	const { v4: uuidv4 } = require('uuid');

	// 当前程序路径
	var workspacePath = path.dirname(process.execPath);
	
	var curWorkspaceId;

	window.onload = function () {

		loadWorkspace();
		
	}

	// txt记录文件信息
	var logFile;

	function exportFileClick(userName) {
		this.disabled=true;
		document.getElementById("loading").style.display = 'inline';
		log("开始打包，请勿进行其他操作！");
		setTimeout(exportFile, 1000, userName);
	}

	/**
	 * 导出文件
	 */
	function exportFile(userName) {
		try {
			// web文件夹名称
			var WebRoot = document.getElementById("webroot").value;
			// 项目路径
			var projectPath = getProjectPath();
			// 打包输出路径
			var exportPath = document.getElementById("exportPath").value;
			exportPath = exportPath.replace(/\\/g,'/');
			if (exportPath.substr(exportPath.length -1) != "/") {
				exportPath += "/";
			}
			exportPath += new Date().getTime() + "/";
			// 打包日志文件
			logFile = exportPath + "export.log";
			// git根路径
			var gitProjectPath = getGitPath(projectPath);
			// git路径比项目路径少的层级
			var gitToProject = projectPath.replace(gitProjectPath, "");
			var fileList = null;
			if (userName) {
				// 按账号筛选
				// 取git日志
				// $ git log --pretty=oneline --since="2022-03-01 00:00:00" --until="2022-03-26 00:00:00"
				var gitLogCommand = 'git log --pretty=oneline --since="'+document.getElementById("startDate").value+' 00:00:00" --until="'+document.getElementById("endDate").value+' 23:59:59" --author="'+userName+'"';
				var gitLog = childProcess.execSync(gitLogCommand, {encoding: 'utf8', cwd: projectPath});
				var gitVersionArr = getAllCommitVersionFromLog(gitLog);
				var allFileList = [];
				for (var i = 0; i < gitVersionArr.length; i++) {
					var fileListCommand = 'git show --name-only --stat --pretty="format:" ' + gitVersionArr[i];
					var versionFileList = childProcess.execSync(fileListCommand, {encoding: 'utf8', cwd: projectPath}).split('\n');
					allFileList = allFileList.concat(versionFileList);
				}
				// 去重
				fileList = [];
				var tempObj = {};
				for (var i = 0; i < allFileList.length; i++) {
					if (!tempObj[allFileList[i]]) {
						tempObj[allFileList[i]] = 1;
						fileList.push(allFileList[i]);
					}
				}
			} else {
				// 全部
				// 取git日志
				// $ git log --pretty=oneline --since="2022-03-01 00:00:00" --until="2022-03-26 00:00:00"
				var gitLogCommand = 'git log --pretty=oneline --since="'+document.getElementById("startDate").value+' 00:00:00" --until="'+document.getElementById("endDate").value+' 23:59:59"';
				var gitLog = childProcess.execSync(gitLogCommand, {encoding: 'utf8', cwd: projectPath});
				var commitVersion = getCommitVersionFromLog(gitLog);
				var fileListCommand = 'git diff --name-only --stat ' + commitVersion[1] + ' ' + commitVersion[0];
				fileList = childProcess.execSync(fileListCommand, {encoding: 'utf8', cwd: projectPath}).split('\n');
			}
			
			// 忽略文件，写在一个字符串里
			var ignoreFileStr = "";
			var ignoreFileInputArray = document.getElementsByName("ignoreFile");
			if (ignoreFileInputArray.length > 0) {
				for (var i = 0; i < ignoreFileInputArray.length; i++) {
					ignoreFileStr += ignoreFileInputArray[i].value.replace(/\\/g,'/');
				}
			}
			for (var i = 0; i < fileList.length; i++) {
				var gitFilePath = fileList[i];
				if (gitFilePath && gitFilePath != '') {
					// 源码路径
					var srcPath = gitFilePath.replace(gitToProject, "");
					if (ignoreFileStr.indexOf(projectPath + srcPath) > -1) {
						log("忽略文件：" + srcPath);
						continue;
					}
					var fromPath = projectPath;
					if (srcPath.indexOf("src") == 0) {
						var newFilePath = srcPath.replace("src", WebRoot + "/WEB-INF/classes");
						var fileType = newFilePath.substr(newFilePath.lastIndexOf(".") + 1);
						if (fileType == 'java') {
							newFilePath = newFilePath.replace(".java", ".class");
							// 复制class
							copyfile(fromPath + newFilePath, exportPath + newFilePath);
							if (fs.existsSync(fromPath + newFilePath.substr(0, newFilePath.lastIndexOf("/")))) {
								var files = fs.readdirSync(fromPath + newFilePath.substr(0, newFilePath.lastIndexOf("/")));
								//遍历读取到的文件列表
								files.forEach(function(filename){
									// 取内部类
									var newFileName = newFilePath.substr(newFilePath.lastIndexOf("/") + 1);
									if (filename.indexOf(newFileName.replace(".class", "$")) > -1) {
										var toPath = exportPath + newFilePath.replace(newFileName, filename);
										copyfile(fromPath + newFilePath.substr(0, newFilePath.lastIndexOf("/")) + '/' + filename, toPath);
									}
								});
							} else {
								log("文件不存在：" + fromPath + newFilePath);
							}
						} else {
							var toPath = exportPath + newFilePath;
							copyfile(fromPath + newFilePath, toPath);
						}
					} else {
						var toPath = exportPath + srcPath;
						copyfile(fromPath + srcPath, toPath);
					}
				}
			}
			log("打包完成！");
		} catch (e) {
			log("打包失败！请检查配置项");
		}
		document.getElementById("loading").style.display = 'none';
		this.disabled=false;
	}

	/**
	 * 取git根路径
	 */
	function getGitPath(projectPath) {
		if (isGitPath(projectPath)) {
			return projectPath;
		} else {
			projectPath = projectPath.substr(0, projectPath.length -1);
			projectPath = projectPath.substr(0, projectPath.lastIndexOf("/") + 1);
			return getGitPath(projectPath);
		}
	}

	/**
	 * 判断路径是否是git根路径
	 */
	function isGitPath(path) {
		var result = false;
		var files = fs.readdirSync(path);
		for (var i = 0; i < files.length; i++) {
			if (files[i] == ".git") {
				result = true;
				break;
			}
		}
		return result;
	}

	/**
	 * 从shell结果中取首次和末次提交版本
	 */
	function getCommitVersionFromLog(gitLog) {
		// 记录字符串转数组
		var logArr = gitLog.split('\n');
		// 取最老版本再往前一个版本
		var gitLogCommand = 'git log --pretty=oneline --until="'+document.getElementById("endDate").value+' 23:59:59" -n ' + logArr.length;
		// 重新生成gitLog
		gitLog = childProcess.execSync(gitLogCommand, {encoding: 'utf8', cwd: getProjectPath()});
		logArr = gitLog.split('\n');
		// 第一个版本（最新版本）
		var firstVersion = logArr[0].split(" ")[0];
		// 最后一个版本（最老版本）
		var lastVersion = logArr[logArr.length - 2].split(" ")[0];
		return [firstVersion, lastVersion];
	}

	function getProjectPath() {
		var projectPath = document.getElementById("projectPath").value;
		projectPath = projectPath.replace(/\\/g,'/');
		if (projectPath.substr(projectPath.length -1) != "/") {
			projectPath += "/";
		}
		return projectPath;
	}

	function getAllCommitVersionFromLog(gitLog) {
		var version = [];
		var logArr = gitLog.split('\n');
		for (var i = 0; i < logArr.length; i++) {
			if (logArr[i] && logArr[i] != "") {
				version.push(logArr[i].split(" ")[0]);
			}
		}
		return version;
	}

	/**
	 * 复制文件
	 */
	function copyfile(src, dir) {
		log("复制文件：" + src);
		try {
			makeDir(dir.substr(0, dir.lastIndexOf("/")));
			fs.writeFileSync(dir,fs.readFileSync(src));
			// txt记录文件信息
			if (logFile) {
				var exportPath = document.getElementById("exportPath").value;
				exportPath = exportPath.replace(/\\/g,'/');
				if (exportPath.substr(exportPath.length -1) != "/") {
					exportPath += "/";
				}
				fs.appendFileSync(logFile, dir.replace(exportPath, "") + "\n");
			}
		} catch (e) {
			log("复制失败：" + src);
		}
	}

	function makeDir(dirpath) {
		if (!fs.existsSync(dirpath)) {
			var pathtmp;
			dirpath.split("/").forEach(function(dirname) {
				if (pathtmp) {
					pathtmp = pathtmp + "/" + dirname;
				}
				else {
	　　　　　　　　　 //如果在linux系统中，第一个dirname的值为空，所以赋值为"/"
					if(dirname){
						pathtmp = dirname;
					}else{
						pathtmp = "/"; 
					}
				}
				if (!fs.existsSync(pathtmp)) {
					if (!fs.mkdirSync(pathtmp)) {
						return false;
					}
				}
			});
		}
		return true;
	}
	
	var win = nw.Window.get();
	var sonWin = null;
	
	win.on('focus', function() {
		if (sonWin != null) {
			sonWin.focus();
			// sonWin.window.aaa();
		}
	});

	win.on('close', function(){
		if (sonWin != null) {
			sonWin.close(true);
		}
		this.close(true);
	});

	function selectPath(selectType, inputId) {
		var url = "selectSysFile.html?selectType=" + selectType + "&inputId=" + inputId;
		var selectPathWindowOptions = {
			"id": "selectSysFile",
			"width": 800,
			"height": 800,
			"position": "center"
		};
		openWindow(url, selectPathWindowOptions);
	}
	
	function openWindow(url, options) {
		var coverDiv = document.createElement("div");
		coverDiv.classList.add("coverAll");
		coverDiv.id = "coverAll";
		document.body.appendChild(coverDiv);
		require('nw.gui').Window.open(url, options, function(new_win) {
			sonWin = new_win;
			new_win.on('loaded', function(){
				sonWin.window.setParentWindow(win);
			});
			new_win.on('close', function(){
				coverDiv.remove();
				sonWin = null;
				this.close(true);
			});
		});
	}
	
	function setPath(path, inputId) {
		document.getElementById(inputId).value = path;
	}

	/**
	 * 输出日志到控制台
	 */
	function log(content) {
		var console = document.getElementById('console');
		var logP = document.createElement('p');
		logP.innerHTML = content;
		console.appendChild(logP);
		console.scrollTop = console.scrollHeight;
	}

	function saveWorkspace() {
		var curWorkspace = {};
		curWorkspace.projectPath = document.getElementById("projectPath").value;
		curWorkspace.webroot = document.getElementById("webroot").value;
		curWorkspace.startDate = document.getElementById("startDate").value;
		curWorkspace.endDate = document.getElementById("endDate").value;
		curWorkspace.exportPath = document.getElementById("exportPath").value;
		curWorkspace.name = document.getElementById(curWorkspaceId).innerHTML;
		curWorkspace.id = curWorkspaceId;
		var ignoreFileInputArray = document.getElementsByName("ignoreFile");
		if (ignoreFileInputArray.length > 0) {
			var ignoreFileArray = [];
			for (var i = 0; i < ignoreFileInputArray.length; i++) {
				ignoreFileArray.push(ignoreFileInputArray[i].value);
			}
			curWorkspace.ignoreFileArray = ignoreFileArray;
		}
		var gitUserNameInputArray = document.getElementsByName("gitUserName");
		if (gitUserNameInputArray.length > 0) {
			var gitUserNameArray = [];
			for (var i = 0; i < gitUserNameInputArray.length; i++) {
				gitUserNameArray.push(gitUserNameInputArray[i].value);
			}
			curWorkspace.gitUserNameArray = gitUserNameArray;
		}
		// 存储工作空间
		var workspace = {};
		if (fs.existsSync(workspacePath + "/workspace.dat")) {
			var workspaceStr = fs.readFileSync(workspacePath + "/workspace.dat");
			workspace = JSON.parse(workspaceStr);
		}
		workspace[curWorkspace.id] = curWorkspace;
		document.getElementById(curWorkspaceId).workspace = curWorkspace;
		// 写入
		fs.writeFileSync(workspacePath + "/workspace.dat",JSON.stringify(workspace));
		// log("保存工作空间成功：" + curWorkspace.name);
		window.setTimeout(function(){
			saveWorkspace();
		}, 1000);
	}

	function showWorkspaceItem(workspaceId) {
		if (curWorkspaceId == workspaceId) {
			return false;
		}
		var curWorkspace = document.getElementById(workspaceId).workspace;
		document.getElementById("projectPath").value = curWorkspace.projectPath;
		document.getElementById("startDate").value = curWorkspace.startDate;
		document.getElementById("endDate").value = curWorkspace.endDate;
		document.getElementById("exportPath").value = curWorkspace.exportPath;
		document.getElementById("webroot").value = curWorkspace.webroot;
		
		curWorkspaceId = workspaceId;
		var ignoreFileDiv = document.getElementById("ignoreFileDiv");
		ignoreFileDiv.innerHTML = "";
		var ignoreFileArray = curWorkspace.ignoreFileArray;
		if (ignoreFileArray && ignoreFileArray.length > 0) {
			for (var i = 0; i < ignoreFileArray.length; i++) {
				var inputId = uuidv4();
				var input = document.createElement("input");
				input.id = inputId;
				input.name = "ignoreFile";
				input.classList.add("ignoreFile");
				input.value = ignoreFileArray[i];
				var button = document.createElement("button");
				button.onclick = function() {
					selectPath('file', input.id);
				}
				button.classList.add("button");
				button.innerHTML = "选择";
				var deleteButton = document.createElement("button");
				deleteButton.onclick = function() {
					this.parentNode.remove();
				}
				deleteButton.classList.add("button");
				deleteButton.classList.add("delete");
				deleteButton.innerHTML = "删除";
				var ignoreFileItemDiv = document.createElement("div");
				ignoreFileItemDiv.appendChild(input);
				ignoreFileItemDiv.appendChild(button);
				ignoreFileItemDiv.appendChild(deleteButton);
				ignoreFileDiv.appendChild(ignoreFileItemDiv);
			}
		}
		var gitUserNameDiv = document.getElementById("gitUserNameDiv");
		gitUserNameDiv.innerHTML = "";
		var gitUserNameArray = curWorkspace.gitUserNameArray;
		if (gitUserNameArray && gitUserNameArray.length > 0) {
			for (var i = 0; i < gitUserNameArray.length; i++) {
				var inputId = uuidv4();
				var input = document.createElement("input");
				input.id = inputId;
				input.name = "gitUserName";
				input.classList.add("gitUserName");
				input.value = gitUserNameArray[i];
				// 导出按钮
				var exportButton = document.createElement("button");
				exportButton.inputId = inputId;
				exportButton.classList.add("button");
				exportButton.innerHTML = "导出";
				exportButton.onclick = function() {
					var userName = document.getElementById(this.inputId).value;
					exportFileClick(userName);
				}
				var deleteButton = document.createElement("button");
				deleteButton.onclick = function() {
					this.parentNode.remove();
				}
				deleteButton.classList.add("button");
				deleteButton.classList.add("delete");
				deleteButton.innerHTML = "删除";
				var gitUserNameItemDiv = document.createElement("div");
				gitUserNameItemDiv.appendChild(input);
				gitUserNameItemDiv.appendChild(exportButton);
				gitUserNameItemDiv.appendChild(deleteButton);
				gitUserNameDiv.appendChild(gitUserNameItemDiv);
			}
		}
		var itemArray = document.getElementById(workspaceId).parentNode.childNodes;
		for(var i = 0; i < itemArray.length; i++){
			if (itemArray[i]) {
				try {
					itemArray[i].classList.remove("active");
				} catch (e) {}
			}
			
		}
		document.getElementById(workspaceId).classList.add("active");
		log("指定工作空间：" + curWorkspace.name);
	}

	function newWorkspace() {
		var newWorkspace = {};
		newWorkspace.projectPath = "";
		newWorkspace.startDate = "";
		newWorkspace.endDate = "";
		newWorkspace.exportPath = "";
		newWorkspace.name = "新工作空间";
		newWorkspace.id = uuidv4();
		// 写入
		var workspace = {};
		if (fs.existsSync(workspacePath + "/workspace.dat")) {
			var workspaceStr = fs.readFileSync(workspacePath + "/workspace.dat");
			workspace = JSON.parse(workspaceStr);
		}
		workspace[newWorkspace.id] = newWorkspace;
		fs.writeFileSync(workspacePath + "/workspace.dat",JSON.stringify(workspace));
		loadWorkspace(newWorkspace.id);
	}

	function deleteWorkspace(workspaceId) {
		if (!workspaceId) {
			workspaceId = curWorkspaceId;
		}
		log("删除工作空间：" + document.getElementById(curWorkspaceId).workspace.name);
		// 存储工作空间
		var workspaceStr = fs.readFileSync(workspacePath + "/workspace.dat");
		var workspace = JSON.parse(workspaceStr);
		delete workspace[curWorkspaceId];
		if (Object.keys(workspace).length > 0) {
			// 写入
			fs.writeFileSync(workspacePath + "/workspace.dat",JSON.stringify(workspace));
		} else {
			// 删除
			fs.unlinkSync(workspacePath + "/workspace.dat");
		}
		// 初始化
		loadWorkspace();
	}

	function loadWorkspace(workspaceId) {
		// 存储工作空间
		var workspaceDiv = document.getElementById("workspaceDiv");
		workspaceDiv.innerHTML = "";
		if (fs.existsSync(workspacePath + "/workspace.dat")) {
			var workspaceStr = fs.readFileSync(workspacePath + "/workspace.dat");
			var workspace = JSON.parse(workspaceStr);
			var i = 0;
			var showId = workspaceId;
			for(var id in workspace){
				var workspaceItem = document.createElement("div");
				workspaceItem.id = id;
				workspaceItem.workspace = workspace[id];
				workspaceItem.classList.add("item");
				workspaceItem.innerHTML = workspace[id].name;
				workspaceItem.setAttribute('contenteditable', true);
				workspaceItem.onclick = function() {
					showWorkspaceItem(this.id);
				}
				workspaceItem.oncontextmenu = function() {
					var curItem = this;
					showWorkspaceItem(curItem.id);
					menuDivRemove();
					var menuDiv = document.createElement("menuDiv");
					menuDiv.id = "menuDiv";
					menuDiv.style.position = "fixed";
					menuDiv.style.width = "100px";
					menuDiv.style.left = curItem.offsetLeft + curItem.offsetWidth;
					menuDiv.style.top = curItem.offsetTop;
					var copyButton = document.createElement("button");
					copyButton.onclick = function() {
						copyWorkspace(curItem);
					}
					copyButton.classList.add("button");
					copyButton.innerHTML = "复制工作空间";
					var deleteButton = document.createElement("button");
					deleteButton.onclick = function() {
						deleteWorkspace(curItem.id);
						// workspaceItem.remove();
					}
					deleteButton.classList.add("button");
					deleteButton.classList.add("delete");
					deleteButton.innerHTML = "删除";
					menuDiv.appendChild(copyButton);
					menuDiv.appendChild(deleteButton);
					document.body.appendChild(menuDiv);
				}
				workspaceDiv.appendChild(workspaceItem);
				if (!showId && i == 0) {
					showId = id;
				}
				i++;
			}
			showWorkspaceItem(showId);
		} else {
			makeDir(workspacePath);
			newWorkspace();
		}
		window.setTimeout(function(){
			saveWorkspace();
		}, 1000);
	}
	
	/**
	 * 添加忽略文件
	 */
	function addIgnoreFile() {
		var inputId = uuidv4();
		var input = document.createElement("input");
		input.id = inputId;
		input.name = "ignoreFile";
		input.classList.add("ignoreFile");
		var button = document.createElement("button");
		button.onclick = function() {
			selectPath('file', inputId);
		}
		button.classList.add("button");
		button.innerHTML = "选择";
		var deleteButton = document.createElement("button");
		deleteButton.onclick = function() {
			this.parentNode.remove();
		}
		deleteButton.classList.add("button");
		deleteButton.classList.add("delete");
		deleteButton.innerHTML = "删除";
		var ignoreFileItemDiv = document.createElement("div");
		ignoreFileItemDiv.appendChild(input);
		ignoreFileItemDiv.appendChild(button);
		ignoreFileItemDiv.appendChild(deleteButton);
		var ignoreFileDiv = document.getElementById("ignoreFileDiv");
		ignoreFileDiv.appendChild(ignoreFileItemDiv);
	}

	function addGitUserName() {
		// git账号输入框
		var inputId = uuidv4();
		var input = document.createElement("input");
		input.id = inputId;
		input.name = "gitUserName";
		input.classList.add("gitUserName");
		// 删除按钮
		var deleteButton = document.createElement("button");
		deleteButton.onclick = function() {
			this.parentNode.remove();
		}
		deleteButton.classList.add("button");
		deleteButton.classList.add("delete");
		deleteButton.innerHTML = "删除";
		// 导出按钮
		var exportButton = document.createElement("button");
		exportButton.inputId = inputId;
		exportButton.classList.add("button");
		exportButton.innerHTML = "导出";
		exportButton.onclick = function() {
			var userName = document.getElementById(this.inputId).value;
			exportFileClick(userName);
		}
		var gitUserNameItemDiv = document.createElement("div");
		gitUserNameItemDiv.appendChild(input);
		gitUserNameItemDiv.appendChild(exportButton);
		gitUserNameItemDiv.appendChild(deleteButton);
		var gitUserNameDiv = document.getElementById("gitUserNameDiv");
		gitUserNameDiv.appendChild(gitUserNameItemDiv);
	}

	function copyWorkspace(workspaceItem) {
		// 复制的工作空间的信息
		var newWorkspace = JSON.parse(JSON.stringify(workspaceItem.workspace));
		newWorkspace.name = newWorkspace.name + "_复制";
		newWorkspace.id = uuidv4();
		// 写入
		var workspace = {};
		if (fs.existsSync(workspacePath + "/workspace.dat")) {
			var workspaceStr = fs.readFileSync(workspacePath + "/workspace.dat");
			workspace = JSON.parse(workspaceStr);
		}
		workspace[newWorkspace.id] = newWorkspace;
		fs.writeFileSync(workspacePath + "/workspace.dat",JSON.stringify(workspace));
		loadWorkspace(newWorkspace.id);
	}
	function bodyClick() {
		menuDivRemove();
		consoleMenuRemove();
	}

	function menuDivRemove() {
		if (document.getElementById("menuDiv")) {
			document.getElementById("menuDiv").remove();
		}
	}

	function consoleMenuRemove() {
		if (document.getElementById("consoleMenu")) {
			document.getElementById("consoleMenu").remove();
		}
	}

	/**
	 * 控制台右键菜单
	 */
	function consoleMenu() {
		consoleMenuRemove();
		var consoleMenu = document.createElement("div");
		consoleMenu.id = "consoleMenu";
		consoleMenu.style.position = "fixed";
		consoleMenu.style.width = "100px";
		consoleMenu.style.left = event.pageX;
		consoleMenu.style.top = event.pageY;
		var clearButton = document.createElement("button");
		clearButton.onclick = function() {
			document.getElementById("console").innerHTML = "";
		}
		clearButton.classList.add("button");
		clearButton.innerHTML = "清除";
		consoleMenu.appendChild(clearButton);
		document.body.appendChild(consoleMenu);
	}
</script>
</html>